<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>待办</title>
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;500;700&family=Rajdhani:wght@500;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --bg-color: #0f172a;
            --card-bg: rgba(30, 41, 59, 0.7);
            --card-border: rgba(255, 255, 255, 0.08);
            --primary: #3b82f6;
            --accent: #8b5cf6;
            --success: #10b981;
            --text-main: #f1f5f9;
            --text-muted: #94a3b8;
            --danger: #ef4444;
        }

        * {
            box-sizing: border-box;
            -webkit-tap-highlight-color: transparent;
        }

        body {
            margin: 0;
            font-family: 'Rajdhani', 'Noto Sans SC', sans-serif;
            background-color: var(--bg-color);
            color: var(--text-main);
            height: 100vh;
            overflow: hidden; /* 防止 body 滚动 */
            display: flex;
            justify-content: center;
            align-items: center;
        }

        /* --- 动态背景 (纯 CSS) --- */
        .ambient-bg {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: -1;
            overflow: hidden;
            background: radial-gradient(circle at 50% 50%, #1e293b 0%, #0f172a 100%);
        }

        .orb {
            position: absolute;
            border-radius: 50%;
            filter: blur(80px);
            opacity: 0.6;
            animation: float 20s infinite ease-in-out;
        }

        .orb-1 {
            width: 300px;
            height: 300px;
            background: var(--primary);
            top: -50px;
            left: -50px;
            animation-delay: 0s;
        }

        .orb-2 {
            width: 400px;
            height: 400px;
            background: var(--accent);
            bottom: -100px;
            right: -100px;
            animation-delay: -5s;
        }

        .orb-3 {
            width: 200px;
            height: 200px;
            background: var(--success);
            top: 40%;
            left: 60%;
            animation-delay: -10s;
            opacity: 0.4;
        }

        @keyframes float {
            0%, 100% { transform: translate(0, 0); }
            33% { transform: translate(30px, 50px); }
            66% { transform: translate(-30px, 20px); }
        }

        /* --- 主容器 --- */
        #app {
            width: 100%;
            height: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .container {
            width: 100%;
            max-width: 500px;
            height: 85vh;
            max-height: 800px;
            background: var(--card-bg);
            backdrop-filter: blur(24px);
            -webkit-backdrop-filter: blur(24px);
            border: 1px solid var(--card-border);
            border-radius: 24px;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
            display: flex;
            flex-direction: column;
            overflow: hidden; /* 关键：限制容器溢出 */
            position: relative;
        }

        /* 移动端适配 */
        @media (max-width: 600px) {
            #app { padding: 0; }
            .container {
                height: 100vh;
                max-height: none;
                border-radius: 0;
                border: none;
            }
        }

        /* --- 头部 --- */
        header {
            padding: 24px 24px 0 24px;
            flex-shrink: 0;
        }

        .app-title {
            font-size: 28px;
            font-weight: 700;
            margin: 0;
            letter-spacing: 1px;
            background: linear-gradient(135deg, #fff 0%, #94a3b8 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .app-stats {
            font-size: 14px;
            color: var(--text-muted);
            font-weight: normal;
            background: rgba(255,255,255,0.1);
            padding: 4px 12px;
            border-radius: 20px;
        }

        /* --- 标签页切换 --- */
        .tabs {
            display: flex;
            padding: 20px 24px;
            gap: 15px;
            flex-shrink: 0;
        }

        .tab-btn {
            background: transparent;
            border: none;
            color: var(--text-muted);
            font-family: inherit;
            font-size: 16px;
            font-weight: 600;
            padding: 8px 0;
            cursor: pointer;
            position: relative;
            transition: color 0.3s;
        }

        .tab-btn.active {
            color: var(--text-main);
        }

        .tab-btn.active::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 3px;
            background: var(--primary);
            border-radius: 3px;
            box-shadow: 0 0 10px var(--primary);
        }

        /* --- 列表区域 (核心滚动修复) --- */
        .list-container {
            flex: 1; /* 占据剩余空间 */
            overflow-y: auto; /* 允许垂直滚动 */
            padding: 0 24px 20px 24px;
            min-height: 0; /* Firefox Flexbox 修复 */
            scroll-behavior: smooth;
        }

        .list-container::-webkit-scrollbar {
            width: 4px;
        }
        .list-container::-webkit-scrollbar-track {
            background: transparent;
        }
        .list-container::-webkit-scrollbar-thumb {
            background: rgba(255,255,255,0.1);
            border-radius: 4px;
        }

        /* --- 列表项 --- */
        .task-item {
            background: rgba(255, 255, 255, 0.03);
            border: 1px solid rgba(255, 255, 255, 0.05);
            border-radius: 16px;
            padding: 16px;
            margin-bottom: 12px;
            display: flex;
            align-items: center;
            transition: all 0.3s ease;
            animation: slideUp 0.3s ease forwards;
            position: relative;
            overflow: hidden;
        }

        .task-item:hover {
            background: rgba(255, 255, 255, 0.06);
            transform: translateY(-2px);
            box-shadow: 0 4px 20px rgba(0,0,0,0.2);
        }

        .checkbox {
            width: 22px;
            height: 22px;
            border: 2px solid var(--text-muted);
            border-radius: 50%;
            margin-right: 14px;
            display: flex;
            justify-content: center;
            align-items: center;
            cursor: pointer;
            transition: all 0.3s;
            flex-shrink: 0;
        }

        .task-content {
            flex: 1;
            min-width: 0;
        }

        .task-text {
            font-size: 16px;
            line-height: 1.4;
            word-break: break-word;
        }

        .task-time {
            font-size: 12px;
            color: var(--text-muted);
            margin-top: 4px;
            display: block;
        }

        /* 完成状态 */
        .task-item.completed {
            opacity: 0.7;
        }
        .task-item.completed .task-text {
            text-decoration: line-through;
            color: var(--text-muted);
        }
        .task-item.completed .checkbox {
            background: var(--success);
            border-color: var(--success);
            box-shadow: 0 0 10px rgba(16, 185, 129, 0.4);
        }
        .task-item.completed .checkbox::after {
            content: '✓';
            color: white;
            font-size: 14px;
            font-weight: bold;
        }

        /* 删除按钮 */
        .delete-btn {
            background: transparent;
            border: none;
            color: var(--text-muted);
            padding: 8px;
            cursor: pointer;
            opacity: 0;
            transition: all 0.2s;
            margin-left: 8px;
            border-radius: 8px;
        }
        
        .task-item:hover .delete-btn {
            opacity: 1;
        }
        
        .delete-btn:hover {
            color: var(--danger);
            background: rgba(239, 68, 68, 0.1);
        }

        /* 移动端优化：总是显示删除按钮但颜色淡 */
        @media (hover: none) {
            .delete-btn { opacity: 1; }
        }

        /* --- 底部输入框 --- */
        .input-area {
            padding: 20px 24px;
            background: rgba(30, 41, 59, 0.9);
            border-top: 1px solid var(--card-border);
            backdrop-filter: blur(10px);
            display: flex;
            gap: 12px;
            flex-shrink: 0; /* 防止被压缩 */
        }

        .input-wrapper {
            flex: 1;
            position: relative;
        }

        input[type="text"] {
            width: 100%;
            padding: 14px 16px;
            background: rgba(0, 0, 0, 0.2);
            border: 1px solid var(--card-border);
            border-radius: 12px;
            color: white;
            font-size: 16px;
            font-family: inherit;
            outline: none;
            transition: all 0.3s;
        }

        input[type="text"]:focus {
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
            background: rgba(0, 0, 0, 0.4);
        }

        .add-btn {
            background: var(--primary);
            color: white;
            border: none;
            border-radius: 12px;
            width: 50px;
            font-size: 24px;
            display: flex;
            justify-content: center;
            align-items: center;
            cursor: pointer;
            transition: all 0.2s;
            box-shadow: 0 0 15px rgba(59, 130, 246, 0.4);
        }

        .add-btn:hover {
            transform: scale(1.05);
            background: #2563eb;
        }
        .add-btn:active { transform: scale(0.95); }

        /* 动画 */
        @keyframes slideUp {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }

        @keyframes fadeOut {
            to { opacity: 0; transform: translateX(20px); }
        }

        .empty-state {
            text-align: center;
            padding: 40px 0;
            color: var(--text-muted);
        }
        .empty-icon {
            font-size: 48px;
            margin-bottom: 10px;
            display: block;
            opacity: 0.5;
        }

    </style>
</head>
<body>

    <!-- 动态背景 -->
    <div class="ambient-bg">
        <div class="orb orb-1"></div>
        <div class="orb orb-2"></div>
        <div class="orb orb-3"></div>
    </div>

    <div id="app">
        <div class="container">
            <header>
                <h1 class="app-title">
                    Flow Tasks
                    <span class="app-stats" id="stats">0 / 0</span>
                </h1>
            </header>

            <div class="tabs">
                <button class="tab-btn active" onclick="switchTab('todo')">待办事项</button>
                <button class="tab-btn" onclick="switchTab('history')">历史记录</button>
            </div>

            <div class="list-container" id="list-container">
                <!-- 任务列表 -->
                <div id="todo-list"></div>
            </div>

            <div class="input-area" id="input-area">
                <div class="input-wrapper">
                    <input type="text" id="task-input" placeholder="添加新任务..." autocomplete="off">
                </div>
                <button class="add-btn" id="add-btn">+</button>
            </div>
        </div>
    </div>

    <script>
        // 数据结构
        let tasks = JSON.parse(localStorage.getItem('flowTasks')) || [
            { id: 1, text: "点击圆圈完成任务", created: Date.now(), completedAt: null },
            { id: 2, text: "滑动查看更多历史", created: Date.now(), completedAt: null }
        ];
        
        let currentTab = 'todo';

        const listContainer = document.getElementById('todo-list');
        const input = document.getElementById('task-input');
        const addBtn = document.getElementById('add-btn');
        const statsEl = document.getElementById('stats');
        const inputArea = document.getElementById('input-area');

        // 初始化
        render();

        function switchTab(tab) {
            currentTab = tab;
            
            // 更新 Tab 样式
            document.querySelectorAll('.tab-btn').forEach(btn => {
                btn.classList.toggle('active', 
                    (tab === 'todo' && btn.innerText.includes('待办')) || 
                    (tab === 'history' && btn.innerText.includes('历史'))
                );
            });

            // 在历史页面隐藏输入框
            if (tab === 'history') {
                inputArea.style.display = 'none';
            } else {
                inputArea.style.display = 'flex';
            }

            render();
        }

        function addTask() {
            const text = input.value.trim();
            if (!text) return;

            const newTask = {
                id: Date.now(),
                text: text,
                created: Date.now(),
                completedAt: null
            };

            tasks.unshift(newTask);
            input.value = '';
            save();
            render();
            
            // 滚动到顶部
            document.querySelector('.list-container').scrollTop = 0;
        }

        function toggleTask(id) {
            const task = tasks.find(t => t.id === id);
            if (task) {
                if (task.completedAt) {
                    // 恢复未完成
                    task.completedAt = null;
                } else {
                    // 标记完成
                    task.completedAt = Date.now();
                    // 可选：完成后自动移到历史？暂时不移，等用户切换
                }
                save();
                render();
            }
        }

        function deleteTask(id) {
            const el = document.getElementById(`task-${id}`);
            if (el) {
                el.style.animation = 'fadeOut 0.3s forwards';
                setTimeout(() => {
                    tasks = tasks.filter(t => t.id !== id);
                    save();
                    render();
                }, 300);
            }
        }

        function formatDate(timestamp) {
            const date = new Date(timestamp);
            return `${date.getMonth() + 1}月${date.getDate()}日 ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
        }

        function render() {
            listContainer.innerHTML = '';

            // 筛选数据
            let filteredTasks = [];
            if (currentTab === 'todo') {
                filteredTasks = tasks.filter(t => !t.completedAt);
            } else {
                filteredTasks = tasks.filter(t => t.completedAt).sort((a, b) => b.completedAt - a.completedAt);
            }

            // 更新统计
            const activeCount = tasks.filter(t => !t.completedAt).length;
            const totalCount = tasks.length;
            statsEl.innerText = `${activeCount} 待办`;

            if (filteredTasks.length === 0) {
                listContainer.innerHTML = `
                    <div class="empty-state">
                        <span class="empty-icon">${currentTab === 'todo' ? '📝' : '🕰️'}</span>
                        ${currentTab === 'todo' ? '所有任务已完成' : '暂无历史记录'}
                    </div>
                `;
                return;
            }

            filteredTasks.forEach(task => {
                const el = document.createElement('div');
                el.className = `task-item ${task.completedAt ? 'completed' : ''}`;
                el.id = `task-${task.id}`;
                
                const timeStr = task.completedAt 
                    ? `完成于: ${formatDate(task.completedAt)}`
                    : `创建于: ${formatDate(task.created)}`;

                el.innerHTML = `
                    <div class="checkbox" onclick="toggleTask(${task.id})"></div>
                    <div class="task-content" onclick="toggleTask(${task.id})">
                        <div class="task-text">${task.text}</div>
                        <small class="task-time">${timeStr}</small>
                    </div>
                    <button class="delete-btn" onclick="deleteTask(${task.id})">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path></svg>
                    </button>
                `;
                listContainer.appendChild(el);
            });
        }

        function save() {
            localStorage.setItem('flowTasks', JSON.stringify(tasks));
        }

        // 事件监听
        addBtn.addEventListener('click', addTask);
        input.addEventListener('keypress', (e) => {
            if (e.key === 'Enter') addTask();
        });
        
        // 全局暴露给 onclick 使用
        window.toggleTask = toggleTask;
        window.deleteTask = deleteTask;
        window.switchTab = switchTab;

    </script>
</body>
</html>